package com.kalua.list.harvester.crawldata;

import java.util.Collection;
import java.util.Collections;
import java.util.Set;
import java.util.concurrent.ConcurrentMap;

import com.google.common.collect.Maps;
import com.google.common.collect.Sets;


public class HistoryImpl implements History {
		
	private Set<String> links;
	private ConcurrentMap<String, ProductLink> productPages;
	
	public HistoryImpl() { 
		this.links = Sets.newConcurrentHashSet();
		this.productPages = Maps.newConcurrentMap();
	}
	
	@Override
	public boolean registerLink(String toFollow) { return links.add(toFollow); }
	
	@Override
	public void saveProductPages(Set<String> metaInformation, 
														Set<String> productLinks) 
	{ 
		ProductLink newLink;
		ProductLink existingMetaData, toUse;
		for (String strLink : productLinks) {
			newLink = new ProductLink(strLink);
			existingMetaData=this.productPages.putIfAbsent(strLink, newLink);
			toUse = existingMetaData!=null? existingMetaData : newLink;
			toUse.addMetaInformation(metaInformation);
		}		
	}
	
	@Override
	public boolean isVisited(String link) { return this.links.contains(link); }
	
	@Override
	public Set<String> getVisited() { return Collections.unmodifiableSet(this.links); }
	
	@Override
	public Set<String> getProductPages() {return this.productPages.keySet();}
	
	@Override
	public Collection<ProductLink> getProductLinks() {return this.productPages.values();}
	
	@Override
	public int linkCount(){return this.links.size();}
	
	@Override
	public int productCount(){return this.productPages.size();}
	
}